package com.wlyuan.open.infrastructure.repository;

import com.wlyuan.mybatis.utils.RepositoryUtils;
import com.wlyuan.open.dao.entity.OpenClientBindDO;
import com.wlyuan.open.dao.mapper.OpenClientBindMapper;
import com.wlyuan.open.domain.client.OpenClientBind;
import com.wlyuan.open.domain.client.OpenClientBindId;
import com.wlyuan.open.domain.client.respository.OpenClientBindRepository;
import com.wlyuan.open.infrastructure.converter.OpenClientBindConverter;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import lombok.var;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Collection;
import java.util.Collections;
import java.util.List;

/**
 * @author yuanzheng
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class OpenClientBindRepositoryImpl implements OpenClientBindRepository {
    private final OpenClientBindMapper openClientBindMapper;

    @Override
    public OpenClientBind ofId(OpenClientBindId openClientBindId) {
        return OpenClientBindConverter.convert(openClientBindMapper.selectById(openClientBindId.getId()));
    }

    @Override
    public OpenClientBindId save(OpenClientBind openClientBind) {
        var clientBindDO = RepositoryUtils.save(openClientBindMapper,
                OpenClientBindConverter::convert,
                openClientBind, openClientBind.getId());

        openClientBind.setId(new OpenClientBindId(clientBindDO.getId()));
        return openClientBind.getId();
    }

    @Override
    public void remove(OpenClientBindId openClientBindId) {

    }

    @Override
    public List<OpenClientBind> list(Collection<OpenClientBindId> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return Collections.emptyList();
        }
        return RepositoryUtils.list(openClientBindMapper,
                OpenClientBindConverter::convert,
                OpenClientBindDO::getId, ids);
    }
}
